package com.tiny.codegen.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.tiny.codegen.common.Column;

public class StringUtil extends StringUtils {

	/**
	 * 将下划线转成驼峰式
	 * 
	 * @param str
	 * @return
	 */
	public static String toClassField(String str) {
		if (str == null || "".equals(str.trim())) {
			return "";
		}
		int len = str.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = str.charAt(i);
			if (c == '_') {
				if (++i < len) {
					sb.append(Character.toUpperCase(str.charAt(i)));
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 将字符串转成类名称
	 * 
	 * @param str
	 * @return
	 */
	public static String toClass(String str) {
		try {
			return StringUtils.capitalize(toClassField(str));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取类型名称
	 * 
	 * @param clazz
	 * @return
	 */
	public static String getClazz(String clazz) {
		if (clazz == null)
			return "";
		if (clazz.lastIndexOf(".") > 0)
			return clazz.substring(clazz.lastIndexOf(".") + 1);
		return clazz;
	}

	/**
	 * 首字母小写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstFiledLower(String str) {
		if (str == null)
			return "";
		return str.substring(0, 1).toLowerCase() + str.substring(1);
	}

	/**
	 * 首字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstFiledUpper(String str) {
		if (str == null)
			return "";
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	/**
	 * 包去重(entity包导入)
	 * 
	 * @return
	 */
	public static List<String> packageDistinct(List<Column> columnClassList) {
		Set<String> set = new HashSet<String>();
		for (Column cc : columnClassList) {
			// 过滤掉lang下的包
			if (cc.getField_type().indexOf("java.lang.") == -1) {
				set.add(cc.getField_type());
			}
		}
		List<String> list = new ArrayList<String>();
		list.addAll(set);
		return list;
	}
}